; CPC Mix Menu v1.7
; (c) 2008-2009 Megachur

USE_COMPACTER		equ 0

;;;;;;;;;;;;;;;;;;;;;
run start_player_in_memory
;;;;;;;;;;;;;;;;;;;;;
;	org &1000
;
;.start_test_player
;
;	di
;	ld bc,&7fc5
;	out (c),c
;	jp start_player_in_memory

; ---------------------------
;write direct -1,-1,&C5
; ---------------------------
	org &4000-&1c
; ---------------------------
	nolist
; ---------------------------
.start_player_in_memory
; ---------------------------
	di

	ld hl,start_player
	ld de,&0100
	ld bc,end_player-start_player+1
	ldir
	ld bc,&7fc5
	out (c),c
	ld hl,&0100
	ld de,start_player
	ld bc,end_player-start_player+1
	ldir
; ---------------------------
.start_player
; ---------------------------
	call wait_vbl

;	di

	ld hl,&c9fb
	ld (&0038),hl

	call init_scan_keyboard

	ld a,(#be7d)			; adresses variables amsdos lecteur courant
	call Set_Drive_Number		; to tell which drive to use
;	add a,65
;	ld (music_insert_disk_drive_text),a
	xor a
	call Set_Drive_Head_Number

	ld sp,&0000

	ld hl,musics_list_on_current_disk
	ld (current_menu_item),hl

	xor a
	ld (current_music_end),a

next_music
	ld bc,&7f9e
	out (c),c
	xor a
	out (c),a
	ld c,&54
	out (c),c
	inc a
	out (c),a
	out (c),c
	ld a,&10
	out (c),a
	out (c),c

	ld a,screen_crtc_address/&100
	ld (crtc_regs+12),a
	ld a,screen_crtc_address
	ld (crtc_regs+13),a

	call set_crtc_regs

	ld hl,screen_first_address
	ld (screen_address),hl

	ld hl,draw_freq_spectrum_analyzer_first_address
	ld (draw_spectrum_analyzer_call_draw),hl
	ld hl,draw_spectrum_analyzer_first_address_table
	ld (draw_spectrum_analyzer_address),hl

	call lbca7

	ld hl,screen_first_address
	ld de,screen_first_address+1
	ld bc,&3fff
	ld (hl),&00
	ldir

	ld hl,&0038
	ld (hl),&c3
	inc hl
	ld (hl),interrupt
	inc hl
	ld (hl),interrupt/&100

	ld bc,&7f01
	out (c),c
	ld a,&4b
	out (c),a

	ei
;
; load next file !
;
	call print_intro_text

load_current_music_index
	call navigation_menu

	ld a,(current_music_end)
	inc a
	jr nz,loop
	call navigation_menu_down
	jr load_current_music_selected_next

loop
	call wait_vbl

	call scan_keyboard

	ld a,(keyboard_a_key_pressed)
	or a
	jr z,loop	; no key pressed ?

first_loop_tempo equ $+1
	ld a,5
	dec a
	ld (first_loop_tempo),a
	jr nz,loop
	ld a,5
	ld (first_loop_tempo),a

	ld a,(keyboard_line_press+keyboard_line_0)
	bit key_up,a
	jr z,not_key_up
	call navigation_menu_up
	jr loop
not_key_up
	bit key_down,a
	jr z,not_key_down
	call navigation_menu_down
	jr loop

not_key_down
	ld a,(keyboard_line_press+keyboard_line_5)
	bit key_space,a
	jr z,not_key_space
	ld hl,(current_menu_item)
	ld a,(hl)
	or a
	jr z,loop
	jr load_current_music_selected_next

not_key_space
	ld a,(keyboard_line_press+keyboard_line_8)
	bit key_a,a
	jr z,not_key_a
	xor a
	jr menu_change_drive_number

not_key_a
	ld a,(keyboard_line_press+keyboard_line_6)
	bit key_b,a
	jr z,not_key_b
	ld a,1

menu_change_drive_number
	call Set_Drive_Number	; select Drive A or B

	add a,65
	ld (music_insert_disk_drive_text),a

not_key_b
	ld a,(keyboard_line_press+keyboard_line_2)
	bit key_return,a
	jr z,loop

refresh_directory
	ld hl,musics_list_on_current_disk
	ld (current_menu_item),hl

	call Load_Directory

;	cp 1	; disk missing
;	call z,chek_Load_Error
	call chek_Load_Error

	call PRINT_LINE_BLANK

load_current_music_selected_next
	xor a
	ld (load_current_music_selected_wait_count),a

load_current_music_selected_wait_count equ $+1
load_current_music_selected_ok
	ld a,&00
	inc a
	ld (load_current_music_selected_wait_count),a
	cp &05
	jr nz,load_current_music_selected_load_ok

	ld a,(load_error_value)
	cp 3	; file not found
	jr nz,load_current_music_selected_load_ok

	jr refresh_directory

load_current_music_selected_load_ok
	xor a
	ld (current_music_end),a

	ld hl,(current_menu_item)
	ld (current_file_load),hl

	ld a,(hl)
	or a	; end of file_list
	jr nz,load_current_music_selected_valid
	ld hl,musics_list_on_current_disk
	ld (current_menu_item),hl
	jp load_current_music_index

load_current_music_selected_valid
	call load_current_music_selected

	ld a,(load_error_value)
	cp &ff
	jp z,load_current_music_index	;loop
	or a
	jr nz,load_current_music_selected_ok

	di

	ld hl,&c9fb
	ld (&0038),hl


	ld hl,l0000
	ld de,&0000
	ld bc,&35
	ldir

	ei

; print game name/year/publisher
; print music author
; check "Megachur" at right place to ensure music data !

	ld hl,music_load+index_Megachur
	ld de,text_Megachur
Megachur_check_continue
	ld a,(de)
	or a
	jr z,Megachur_check_ok
	cp (hl)
	jr nz,Megachur_check_fails
	inc hl
	inc de
	jr Megachur_check_continue

Megachur_check_fails
;	rst 0
	ld hl,text_not_musicfile
	call PRINT_TEXT
	ld hl,&8000
not_musicfile_loop
	dec hl
	ld a,h
	or l
	jr nz,not_musicfile_loop
	call navigation_menu_down
	jp next_music

Megachur_check_ok

; check that screen address = &c000 is free or disable writing char

	ld a,(File_NbBlock)
	cp &80
	jr nz,file_loaded_is_not_to_long

	ld c,0

	ld hl,File_Block_Table+16
count_number_of_extended_block
	ld a,(hl)
	inc hl
	or a
	jr z,no_more_extended_block
	inc c
	jr count_number_of_extended_block
no_more_extended_block

	ld a,c
	and &0f		; only &3c00 long available !!!

	ld (no_more_extended_block_restore_nb),a

	ld hl,screen_first_address

	push hl
	push af

	ld de,file_temp_memory_adr_save
;
; copy extended block to file_temp_memory_adr_save = &0400
;
move_memory
	ld bc,&0400
	ldir
	dec a
	jr nz,move_memory

	pop af
	pop hl

	ld d,h
	ld e,l
clear_move_memory
	inc de
	ld (hl),&00
	ld bc,&0400-1
	ldir
	inc hl
	dec a
	jr nz,clear_move_memory

file_loaded_is_not_to_long
	call PRINT_LINE_BLANK

	ld hl,(music_load)
	ld bc,28
	add hl,bc
	ld (music_address_begin),hl

	ld hl,(music_load+index_game_name)
	ld bc,(music_load)
	and a	; reset carry
	sbc hl,bc
	ld bc,28
	and a	; reset carry
	sbc hl,bc
	ld (length_of_music_withoutcomment),hl
	add hl,bc

	ld a,(File_NbBlock)
	cp &80
	jr nz,find_information_in_first_16ko
	ld de,file_temp_memory_adr_save-&4000	; warning if comment is cut (first in &7fff then &0400)
	jr find_information

find_information_in_first_16ko
	ld de,music_load

find_information
	add hl,de
;
; music name
;
	call PRINT_TEXT

	ld a," "
	call PRINT_CHAR
	ld a,"("
	call PRINT_CHAR
	call PRINT_TEXT
	ld a,")"
	call PRINT_CHAR

	ld a," "
	call PRINT_CHAR
	ld a,"("
	call PRINT_CHAR
	call PRINT_TEXT
	ld a,")"
	call PRINT_CHAR

	call PRINT_TEXT_CRx2
;
; music author
;
;	push hl
	ex de,hl
	ld hl,text_music_author
	call PRINT_TEXT
	ex de,hl
;	pop hl

	ld a,(hl)
	or a
	jr nz,known_author
;	push hl
	ex de,hl
	ld hl,text_unknown_author
	call PRINT_TEXT
;	pop hl
	ex de,hl
	inc hl
	jr unknown_author

known_author
	call PRINT_TEXT
unknown_author
	call PRINT_TEXT_CRx2

	ld a,(hl)
	or a
	jr nz,no_comments
;	push hl
	ex de,hl
	ld hl,text_no_comments
	call PRINT_TEXT
	ex de,hl
;	pop hl
	jr comments

no_comments
	call PRINT_TEXT
comments
	call PRINT_TEXT_CRx2
;
; Date rip
;
	ld hl,text_music_rip
	call PRINT_TEXT
	ld hl,text_music_date_rip
	call PRINT_TEXT
	ld a,(music_load+index_music_date_rip+1)
	ld (Number_To_Print),a
	call PRINT_NUMBER
	ld a,"/"
	call PRINT_CHAR
	ld a,(music_load+index_music_date_rip)
	and &f0
	rra
	rra
	rra
	rra
	ld (Number_To_Print),a
	call PRINT_NUMBER
	ld a,"/"
	call PRINT_CHAR
	ld a,20
	ld (Number_To_Print+1),a
	ld a,(music_load+index_music_date_rip)
	and &0f
	ld (Number_To_Print),a
	call PRINT_NUMBER_Double

	call PRINT_TEXT_CRx2
;
; music address (begin, play, init, stop, end, length)
;
	ld hl,(music_load+index_music_real_adr)
	ld (Hexa_Number_To_Print),hl
	ld hl,text_music_real_adr
	call PRINT_TEXT
	call PRINT_HEXA_NUMBER

	ld hl,(music_load+index_init_music)
	ld (call_init_music_adr),hl
	ld (Hexa_Number_To_Print),hl
	ld hl,text_init_music
	call PRINT_TEXT
	call PRINT_HEXA_NUMBER

	ld hl,(music_load+index_play_music)
	ld (call_play_music_adr),hl
	ld (Hexa_Number_To_Print),hl
	ld hl,text_play_music
	call PRINT_TEXT
	call PRINT_HEXA_NUMBER

	ld hl,(music_load+index_stop_music)
	ld (Hexa_Number_To_Print),hl
	ld hl,text_stop_music
	call PRINT_TEXT
	call PRINT_HEXA_NUMBER

	ld hl,(music_load+index_music_end)
	ld (Hexa_Number_To_Print),hl
	ld hl,text_music_end
	call PRINT_TEXT
	call PRINT_HEXA_NUMBER
;
; music comment
;
	ld hl,(length_of_music_withoutcomment)
	ld (Hexa_Number_To_Print),hl
	ld hl,text_music_length
	call PRINT_TEXT
	call PRINT_HEXA_NUMBER

	call PRINT_TEXT_CRx2

	ld hl,(music_load+index_music_end)
	ld (music_end_address),hl

	ld hl,music_load+index_last_theme
	ld a,(hl)
	ld (current_music_max_theme),a

	ld hl,music_load+index_first_theme
	ld a,(hl)
	ld (current_music_first_theme),a
	call display_current_theme

	ld de,(screen_address)
	call print_intro_text
	ld (screen_address),de
;
; copy screen to bank 6
;
	ld de,&4000

	ld hl,move_function_memory_adr
	ld (hl),&01
	inc hl
	ld (hl),bank_6
	inc hl
	ld (hl),&7f
	inc hl
	ld (hl),&ed
	inc hl
	ld (hl),&49
	inc hl
	ld (hl),&21
	inc hl
	ld (hl),screen_first_address
	inc hl
	ld (hl),screen_first_address/&100
	inc hl
	ld (hl),&01
	inc hl
	ld (hl),&4000
	inc hl
	ld (hl),&4000/&100
	inc hl
	ld (hl),&ed
	inc hl
	ld (hl),&b0
	inc hl
	ld (hl),&c3
	inc hl
	ld (hl),bank_5
	inc hl
	ld (hl),bank_5/&100
	call move_function_memory_adr
;
; check that screen address = &c000 is free
; move memory if necessary
;
	ld a,(File_NbBlock)
	cp &80
	jp nz,file_loaded_is_not_to_long_restore

no_more_extended_block_restore_nb equ $+1
	ld a,&00

	ld hl,file_temp_memory_adr_save
	ld de,screen_first_address
move_memory_restore
	ld bc,&400
	ldir
	dec a
	jr nz,move_memory_restore

file_loaded_is_not_to_long_restore
	ld hl,(music_address_begin)
	ld bc,screen_first_address
	and a	; reset carry
	sbc hl,bc
	jp nc,music_adrbegin_sup_c000

; begin < &c000

	ld hl,(music_address_begin)
	ld bc,(length_of_music_withoutcomment)
	add hl,bc
	ld bc,screen_first_address
	and a	; reset carry
	sbc hl,bc
	jp nc,music_adrend_sup_c000

; end < &c000

	ld hl,screen_first_address

	jr copy_file_memory_to_right_place

music_adrend_sup_c000
	ld hl,(music_address_begin)
	ld bc,screen_second_address
	and a	; reset carry
	sbc hl,bc
	jp nc,music_adrbegin_sup_8000

music_adrbegin_sup_c000
; begin < &8000

	ld hl,(music_address_begin)
	ld bc,(length_of_music_withoutcomment)
	add hl,bc
	ld bc,screen_second_address+&4000
	and a	; reset carry
	sbc hl,bc
	jp c,music_adrend_sup_8000

; end < &8000

	ld a,(screen_address+1)
	add &c0
	ld (screen_address+1),a

	ld a,screen_crtc_2nd_address/&100
	ld (crtc_regs+12),a
	ld a,screen_crtc_2nd_address
	ld (crtc_regs+13),a

	ld hl,draw_freq_spectrum_analyzer_second_address
	ld (draw_spectrum_analyzer_call_draw),hl
	ld hl,draw_spectrum_analyzer_second_address_table
	ld (draw_spectrum_analyzer_address),hl

	ld hl,screen_second_address

	jr copy_file_memory_to_right_place

music_adrbegin_sup_8000
music_adrend_sup_8000
	ld a,(screen_address+1)
	add &40
	ld (screen_address+1),a

	ld a,screen_crtc_3rd_address/&100
	ld (crtc_regs+12),a
	ld a,screen_crtc_3rd_address
	ld (crtc_regs+13),a

	ld hl,draw_freq_spectrum_analyzer_third_address
	ld (draw_spectrum_analyzer_call_draw),hl
	ld hl,draw_spectrum_analyzer_third_address_table
	ld (draw_spectrum_analyzer_address),hl

	ld a,&01
	ld (screen_third_activate),a

	ld hl,screen_third_address+minimum_preserve_memory_length

copy_file_memory_to_right_place
	ld (screen_address_selected),hl

music_address_begin equ $+1
	ld hl,0 ;(music_load)+28

	ld de,music_load+28
	ld a,e	; music_load+28
	cp l
	jr nz,music_adr_ko
	ld a,d	; music_load+28/&100
	cp h
	jp z,music_load_memory_last
music_adr_ko
	ex de,hl

	push hl

;	ld hl,music_load+28
	ld b,d
	ld c,e	; (music_load)
	and a	; reset carry
	sbc hl,bc

length_of_music_withoutcomment equ $+1
	ld bc,0	;(music_load+index_EOF_LENGTH)

	jr nc,music_load_memory_ldir
	dec bc
	pop hl
	add hl,bc
	ex de,hl
	add hl,bc
	ex de,hl
	inc bc
	call lddr_music
	jr music_load_memory_last

music_load_memory_ldir
	pop hl
	call ldir_music
;
; check that screen address = &c000 is free
;
music_load_memory_last

screen_address_selected equ $+1
	ld de,&0

	ld hl,move_function_memory_adr
	ld (hl),&01
	inc hl
	ld (hl),bank_6
	inc hl
	ld (hl),&7f
	inc hl
	ld (hl),&ed
	inc hl
	ld (hl),&49
	inc hl
	ld (hl),&21
	inc hl
	ld (hl),&4000
	inc hl
	ld (hl),&4000/&100
screen_third_activate equ $+1
	ld a,0
	dec a
	jr nz,screen_third_not_activate
	ld (screen_third_activate),a
	dec hl
	ld (hl),&4000+minimum_preserve_memory_length
	inc hl
	ld (hl),&4000+minimum_preserve_memory_length/&100

screen_third_not_activate
	inc hl
	ld (hl),&01
	inc hl
	ld (hl),&4000-minimum_preserve_memory_length
	inc hl
	ld (hl),&4000-minimum_preserve_memory_length/&100	; preserve stack or before &0038
	inc hl
	ld (hl),&ed
	inc hl
	ld (hl),&b0
	inc hl
	ld (hl),&c3
	inc hl
	ld (hl),bank_5
	inc hl
	ld (hl),bank_5/&100
	call move_function_memory_adr
;
; music_adr_ok and screen_ok
;
	call set_crtc_regs

	ld de,(screen_address)

	ld hl,text_music_timer_count_first
	call PRINT_TEXT

	ld hl,text_spectrum_analyzer
	call PRINT_TEXT

	ld a,(print_psg_values)
	cp &21
	jr nz,not_print_text_psg_values
	ld hl,text_psg_values
	call PRINT_TEXT

not_print_text_psg_values
	ld (screen_address),de

	ld a,(current_music_first_theme)
	call change_current_theme

	ei

;-------------------------------------------------------------------------------
play_music_wait_vbl
;-------------------------------------------------------------------------------
	call wait_vbl

	call call_play_music

	or a
	jr nz,not_current_theme_end

	call change_theme_inc

not_current_theme_end

	call read_print_psg_values

	call draw_spectrum_analyzer

	call scan_keyboard

	ld a,(keyboard_a_key_pressed)
	or a
	jp z,test_key_end	; no key pressed
tempo equ $+1
	ld a,5
	dec a
	ld (tempo),a
	jp nz,test_key_end

	ld a,5
	ld (tempo),a

	ld a,(keyboard_line_press+keyboard_line_8)
	bit key_esc,a
	jp nz,next_music

test_key_space
;
; load and play next music
;
	ld a,(keyboard_line_press+keyboard_line_5)
	bit key_space,a
	jr z,test_key_left

	ld a,&ff
	ld (current_music_end),a

test_key_left
;
; play the previous theme of the current music
;
	ld a,(keyboard_line_press+keyboard_line_1)
	bit key_left,a
	jr z,test_key_right

	call change_theme_dec

test_key_right
;
; play the next theme of the current music
; if it is the last theme of the current music
; and
; if the music loop is disable
; then load and play next music (same as press "space")
; else play first theme of the current music
;
	ld a,(keyboard_line_press+keyboard_line_0)
	bit key_right,a
	jr z,test_key_r

	call change_theme_inc

test_key_r
;
; en/disable music loop
;
	ld a,(keyboard_line_press+keyboard_line_6)
	bit key_r,a
	jr z,test_key_f
	ld a,(set_music_loop_test)
	xor &ff
	ld (set_music_loop_test),a

	ld de,(screen_address)
	or a
	jr z,clear_text_music_repeat
	ld hl,text_music_repeat
	jr print_text_music_repeat_end
clear_text_music_repeat
	ld hl,text_music_repeat_clear
print_text_music_repeat_end
	call PRINT_TEXT
	ld (screen_address),de

test_key_f
;
; en/disable a duration of only three minutes of the theme of the music
;
	ld a,(keyboard_line_press+keyboard_line_6)
	bit key_f,a
	jr z,test_key_p
	ld a,(force_currentmusic_duration_active)
	xor &01
	ld (force_currentmusic_duration_active),a

	ld de,(screen_address)
	or a
	jr z,clear_text_music_force_end
	ld hl,text_music_force_end
	jr print_text_music_force_end_end
clear_text_music_force_end
	ld hl,text_music_force_end_clear
print_text_music_force_end_end
	call PRINT_TEXT
	ld (screen_address),de

	call set_force_currentmusic_duration_timer

test_key_p
;
; activate display of PSG registers
; disable the display of spectrum analyzer
;
	ld a,(keyboard_line_press+keyboard_line_3)
	bit key_p,a
	jr z,test_key_s

	ld a,&c9
	ld (draw_spectrum_analyzer),a
	ld a,&21
	ld (print_psg_values),a

	ld de,(screen_address)
	ld hl,text_psg_values
	call PRINT_TEXT
	ld (screen_address),de


test_key_s
;
; activate the display of spectrum analyzer
; disable display of PSG registers
;
	ld a,(keyboard_line_press+keyboard_line_7)
	bit key_s,a
	jr z,test_key_end

	ld a,&c9
	ld (print_psg_values),a
	ld a,&cd
	ld (draw_spectrum_analyzer),a

test_key_end
	call time_counter

	ld de,(screen_address)
	call draw_time_counter
	ld (screen_address),de

force_currentmusic_duration_active equ $+1
	ld a,0
	or a
	jr z,no_force_currentmusic_duration
	ld hl,time_counter_value+2
	ld a,(hl)
force_currentmusic_duration_counter equ $+1
	cp FORCE_CURRENTMUSIC_DURATION
	jr nz,test_inactivity_of_music
	dec hl
	ld a,(hl)
force_currentmusic_duration_counter_secs equ $+1
	cp 0
	jr nz,test_inactivity_of_music
	dec hl
	ld a,(hl)
force_currentmusic_duration_counter_frame equ $+1
	cp 0
	call z,change_theme_inc
;
test_inactivity_of_music
;
; test inactivity of music every second !
;
	ld hl,(read_psg_value+8)
last_psg_value8 equ $+1
	ld de,0
	ld (last_psg_value8),hl
	ld a,h
	cp d
	jr nz,reset_psg_value_same_as_last_vbl
	ld a,l
	cp e
	jr nz,reset_psg_value_same_as_last_vbl

	ld a,(read_psg_value+10)
last_psg_value10 equ $+1
	ld b,0
	ld (last_psg_value10),a
	cp b
	jr nz,reset_psg_value_same_as_last_vbl

	ld hl,(read_psg_value+0)
last_psg_value0 equ $+1
	ld de,0
	ld (last_psg_value0),hl
	ld a,h
	cp d
	jr nz,reset_psg_value_same_as_last_vbl
	ld a,l
	cp e
	jr nz,reset_psg_value_same_as_last_vbl

	ld hl,(read_psg_value+2)
last_psg_value2 equ $+1
	ld de,0
	ld (last_psg_value2),hl
	ld a,h
	cp d
	jr nz,reset_psg_value_same_as_last_vbl
	ld a,l
	cp e
	jr nz,reset_psg_value_same_as_last_vbl

	ld hl,(read_psg_value+4)
last_psg_value4 equ $+1
	ld de,0
	ld (last_psg_value4),hl
	ld a,h
	cp d
	jr nz,reset_psg_value_same_as_last_vbl
	ld a,l
	cp e
	jr nz,reset_psg_value_same_as_last_vbl

psg_value_same_as_last_vbl equ $+1
	ld a,0
	inc a
	ld (psg_value_same_as_last_vbl),a
	jr nz,play_music_wait_vbl_test_music_end

	call change_theme_inc

reset_psg_value_same_as_last_vbl
	xor a
	ld (psg_value_same_as_last_vbl),a

play_music_wait_vbl_test_music_end
no_force_currentmusic_duration
;
; test_current_theme_end
;
	ld a,(current_music_end)
	or a
	jp z,play_music_wait_vbl
	ld a,(current_music_end)
set_music_loop_test	equ $+1
	cp 0
	jp nz,next_music
	xor a
	ld (current_music_end),a

	jp play_music_wait_vbl
;-------------------------------------------------------------------------------
.change_theme_dec
;-------------------------------------------------------------------------------
	ld a,(current_music_first_theme)
	dec a
	ld b,a
	ld a,(current_music_theme)
	dec a
	cp b
	jr nz,change_current_theme
	ld a,(current_music_max_theme)
	jr change_current_theme
;-------------------------------------------------------------------------------
.change_theme_inc
;-------------------------------------------------------------------------------
	ld a,(current_music_max_theme)
	inc a
	ld b,a
	ld a,(current_music_theme)
	inc a
	cp b
	jr c,change_current_theme
	ld a,&ff
	ld (current_music_end),a

	ld a,(current_music_first_theme)
;-------------------------------------------------------------------------------
change_current_theme
;-------------------------------------------------------------------------------
	ld (current_music_theme),a
	call display_current_theme

	ld hl,0
	ld (time_counter_value),hl
	ld (time_counter_value+2),hl

	ld a,(current_music_theme)

	call call_init_music
;------------------------
set_force_currentmusic_duration_timer
;------------------------
	ld hl,time_counter_value
	ld a,(hl)	; get frames
	ld (force_currentmusic_duration_counter_frame),a
	inc hl
	ld a,(hl)	; get seconds
	ld (force_currentmusic_duration_counter_secs),a
	inc hl
	ld a,(hl)	; get minute
	add a,FORCE_CURRENTMUSIC_DURATION
	ld (force_currentmusic_duration_counter),a
	ret
;------------------------
.display_current_theme
;------------------------
	ld (Number_To_Print),a

	ld hl,text_playing_theme
	call PRINT_TEXT

	call PRINT_NUMBER
	ld a,"/"
	call PRINT_CHAR
	ld a,(current_music_max_theme)
	ld (Number_To_Print),a
	call PRINT_NUMBER
	ld a,13
	jp PRINT_CHAR
;-------------------------------------------------------------------------------
.read_print_psg_values
;-------------------------------------------------------------------------------
	ld hl,read_psg_value+10
	ld d,10

	di

.read_data_to_psg
	ld b,#f4
	out (c),d
	ld b,#f6
	in a,(c)
	or #c0
	out (c),a
	and #3f
	out (c),a

	inc b
;	ld bc,&f792
	ld c,%10010010
	out (c),c   ; Port &F4xx en lecture
	dec b

;	ld b,#f6
;	ld c,a
	or #40
	out (c),a
;	out (c),c

	ld b,#f4
	in c,(c)
	ld (hl),c

	ld bc,&f700+%10000010
	out (c),c   ; Port &F4xx en ecriture

	dec hl
	dec d
	jp p,read_data_to_psg

	ld bc,&f600
	out (c),c

	ei
; ---------------------------
.print_psg_values
; ---------------------------
;	ld hl,psg_values_text
	ret
	dw text_locate_psg_values

	ld de,(screen_address)
	
	call PRINT_TEXT
	ld hl,(psg_read_data_reg0)
	ld (Hexa_Number_To_Print),hl
	call PRINT_HEXA_NUMBER
	ld a,32
	call PRINT_CHAR
	ld hl,(psg_read_data_reg2)
	ld (Hexa_Number_To_Print),hl
	call PRINT_HEXA_NUMBER
	ld a,32
	call PRINT_CHAR
	ld hl,(psg_read_data_reg4)
	ld (Hexa_Number_To_Print),hl
	call PRINT_HEXA_NUMBER
	ld a,32
	call PRINT_CHAR
	ld a,(psg_read_data_reg6)
	ld (Hexa_Number_To_Print),a
	call PRINT_HEXA_NUMBER_simple
	ld a,32
	call PRINT_CHAR
	ld a,(psg_read_data_reg7)
	ld (Hexa_Number_To_Print),a
	call PRINT_HEXA_NUMBER_simple
	ld a,32
	call PRINT_CHAR
	ld a,(psg_read_data_reg8)
	ld (Number_To_Print),a
	call PRINT_NUMBER
	ld a,32
	call PRINT_CHAR
	ld a,(psg_read_data_reg9)
	ld (Number_To_Print),a
	call PRINT_NUMBER
	ld a,32
	call PRINT_CHAR
	ld a,(psg_read_data_rega)
	ld (Number_To_Print),a
	call PRINT_NUMBER
	ld a,13
	call PRINT_CHAR
	ld a,11
	call PRINT_CHAR
	ld a,11
	call PRINT_CHAR
	ld (screen_address),de
	ret
psg_read_data_rega	equ $+10
psg_read_data_reg9	equ $+09
psg_read_data_reg8	equ $+08
psg_read_data_reg7	equ $+07
psg_read_data_reg6	equ $+06
psg_read_data_reg4	equ $+04
psg_read_data_reg2	equ $+02
psg_read_data_reg0	equ $+00
read_psg_value
	defs 11,0
; ---------------------------
.load_directory
; ---------------------------
; Out
; a = Return State
;	0 -> File Found
;	1 -> Disc Missing
;	2 -> Read Fail
;	3 -> File Not Found
;	a = &e5 -> End of Directory -> File Not Found
; hl = Ptr On Memory Of The First Entry Of Directory Matched
; ---------------------------
	ld hl,loading_directory_text
	call PRINT_TEXT
	ld hl,music_selected_disk_text
	call PRINT_TEXT

	ld hl,music_load
	ld (Memory_Directory_Data),hl

	call Go_To_First_Track_Of_The_Directory
	ret nz

	ld hl,musics_list_on_current_disk
	ld (load_directory_musiclist_index),hl
;
; Get Data Of Directory Sector (First to Last)
;
	ld a,DIRECTORY_FIRST_SECTOR

Load_Directory_next_sector
	ld (Load_Directory_next_sector_index),a	; current_directory_sector

	call Get_Data_Of_Directory_Sector
	ret nz

	ld hl,music_load

load_directory_musiclist_index equ $+1
	ld de,musics_list_on_current_disk

load_directory_get_next_file
	ld (load_directory_get_next_file_index),a

	ld a,(hl)
	inc hl
	cp &e5	; file deleted
	jr nz,find_same_file_noskip
	ld a,(hl)
	cp &e5	; end of directory
	jr z,Load_Directory_end
	jr find_same_file_skip_file_deleted

find_same_file_noskip
	push hl
	push de

	ld bc,11
	ex de,hl
	and a	; reset carry
	sbc hl,bc

	call Find_Same_Filename

	pop de
	pop hl

	jr nz,find_same_file_text_not_match

find_same_file_skip_file_deleted
find_same_file_skip
	ld bc,11
	add hl,bc

	jr find_same_file_skip_same_entry

find_same_file_text_not_match
	ld bc,11
	ldir

find_same_file_skip_same_entry
	ld bc,&20-11-1
	add hl,bc

load_directory_get_next_file_index equ $+1
	ld a,&00
	inc a
	cp 16
	jr nz,load_directory_get_next_file

	ld (load_directory_musiclist_index),de
;
;.Get_Data_Of_Directory_Next_Sector
;
Load_Directory_next_sector_index equ $+1
	ld a,&00
	inc a
	cp DIRECTORY_LAST_SECTOR
	jr nz,Load_Directory_next_sector	; reach end of directory ?

Load_Directory_end
	xor a
	ld (de),a	; end of list
	ret
; ---------------------------
load_current_music_selected
; ---------------------------
;	push hl
	ex de,hl

	ld hl,loading_text
	call PRINT_TEXT

	call print_filename

	ex de,hl
;	pop hl

	IF USE_COMPACTER

	ld de,PACK_ADDRESS
	call OPEN_FILE

	ld a,(load_error_value)
	or a
	jr nz,load_current_music_selected_nodepack

	ld hl,decrunching_text
	call PRINT_TEXT

	ld hl,PACK_ADDRESS
	ld de,music_load

	jp depack

load_current_music_selected_nodepack

	ELSE

	ld de,music_load
	jp OPEN_FILE

	ENDIF

;-------------------------------------------------------------------------------
.OPEN_FILE
;-------------------------------------------------------------------------------
; In
;	hl = Filename Ptr
;	de = Buffer Address
; ---------------------------
; Out
; a = Return State
;	0 -> File Loaded
;	1 -> Disc Missing
;	2 -> Read Fail
;	3 -> File Not Found
; ---------------------------
	xor a
	ld (load_error_value),a

	ld b,d
	ld c,e

	call LOADFILE
;
.chek_Load_Error
;
	ld (load_error_value),a

	or a
	jp z,Set_Motor_Off	; end loading with no error

	cp 1
	jr z,load_error_file_missing

	cp 3
	jr z,load_error_file_missing

	ld a,FDC_Command_Recalibrate
	call FDC_Command

	jr error_load_wait_return_text

load_error_file_missing
;
; Try the other head of the drive if drive is two side
;
	ld a,(Disk_Drive_Two_Side)
	or a
	jr nz,error_load_wait_return_text

	ld a,(HDSDS1DS0)
	and 4
	xor 4
	sra a
	sra a

	push af

	call Set_Drive_Head_Number	; to tell which drive to use, head to read.

	pop af
	add a,48
	ld (music_insert_disk_drive_head_text),a

error_load_wait_return_text
	ld hl,text_cr
	call PRINT_TEXT

error_load_wait
error_load_wait_number  equ $+1
	ld a,0
	inc a
	ld (error_load_wait_number),a
	cp &10
	jr nz,error_load_wait_keys
	xor a
	ld (error_load_wait_number),a

error_load_wait_text_swap equ $+1
	ld a,0
	xor 1
	ld (error_load_wait_text_swap),a
	or a
	jr nz,error_load_print_blank_line

error_load_wait_retry	equ $+1
	ld a,0
	inc a
	ld (error_load_wait_retry),a
	cp &10/2
	jr z,error_load_end

	ld hl,music_insert_disk_text
	call PRINT_TEXT
	jr error_load_wait_keys

error_load_print_blank_line
	call PRINT_LINE_BLANK

error_load_wait_keys
	call wait_vbl

	call scan_keyboard

	ld a,(keyboard_a_key_pressed)
	or a
	jr z,error_load_wait

	ld a,(keyboard_line_press+keyboard_line_8)
	bit key_esc,a
	jr nz,error_load_stop

	bit key_a,a
	jr z,error_load_not_key_a
	xor a
	jr error_load_change_drive

error_load_not_key_a
	ld a,(keyboard_line_press+keyboard_line_6)
	bit key_b,a
	jr z,error_load_end
	ld a,1

error_load_change_drive
	call Set_Drive_Number	; Drive A or B

	add a,65
	ld (music_insert_disk_drive_text),a

	jr error_load_end

error_load_stop
	ld a,&ff
	ld (load_error_value),a

error_load_end
	call PRINT_LINE_BLANK

	xor a
	ld (error_load_wait_retry),a
	ld (error_load_wait_number),a

	ld a,11
	call PRINT_CHAR

	jp Set_Motor_Off
;
load_error_value
	db 0
; ---------------------------
.navigation_menu_up
; ---------------------------
; previous music !
; ---------------------------
	ld hl,(current_menu_item)
	ld a,h
	cp musics_list_on_current_disk/&100
	jr nz,not_first_menu_item
	ld a,l
	cp musics_list_on_current_disk
	jr nz,not_first_menu_item
;	ld hl,musics_list_on_current_disk_end
	jr navigation_menu_print_filename

not_first_menu_item
	ld bc,11
	and a	; reset carry
	sbc hl,bc

	jr navigation_menu_print_filename
; ---------------------------
.navigation_menu_down
; ---------------------------
; next music !
; ---------------------------
	ld hl,(current_menu_item)
	ld a,h
	cp musics_list_on_current_disk_end/&100
	jr nz,not_last_menu_item
	ld a,l
	cp musics_list_on_current_disk_end
	jr nz,not_last_menu_item
last_menu_item
	ld hl,musics_list_on_current_disk
	jr navigation_menu_print_filename

not_last_menu_item
	ld bc,11
	add hl,bc

	ld a,(hl)
	or a
	jr z,last_menu_item

navigation_menu_print_filename
	ld (current_menu_item),hl

	ld a,11		; menu_up_text
	call PRINT_CHAR
;	jp navigation_menu
; ---------------------------
.navigation_menu
; ---------------------------
	call PRINT_LINE_BLANK

	call print_filename

	ld a,10
	jp PRINT_CHAR
; ---------------------------
.print_filename
; ---------------------------
	ld hl,(current_menu_item)

	ld a,(hl)
	or a
	jr z,print_filename_empty

	ld b,11-3
navigation_menu_print_filename_loop
	ld a,(hl)
	inc hl
	call PRINT_CHAR
	djnz navigation_menu_print_filename_loop

	ld a,"."
	call PRINT_CHAR

	ld b,3
navigation_menu_print_filename_extension_loop
	ld a,(hl)
	inc hl
	call PRINT_CHAR
	djnz navigation_menu_print_filename_extension_loop

print_filename_empty
	ret
; ---------------------------
.draw_spectrum_analyzer
; ---------------------------
draw_spectrum_analyzer_call_draw equ $+1
	call draw_freq_spectrum_analyzer_first_address

	ld a,03
	ld (draw_freq_pixel),a

	ld hl,(psg_read_data_reg0)
	ld a,(psg_read_data_reg8)

	call calculate_freq_spectrum_analyzer

	ld a,24
	ld (draw_freq_pixel),a

	ld hl,(psg_read_data_reg2)
	ld a,(psg_read_data_reg9)

	call calculate_freq_spectrum_analyzer

	ld a,192
	ld (draw_freq_pixel),a

	ld hl,(psg_read_data_reg4)
	ld a,(psg_read_data_rega)

calculate_freq_spectrum_analyzer
	cp &10
	jr nz,not_use_psg_preset
	ld a,&0f

not_use_psg_preset
	and &0f
	ret z
	ld (psg_channel_volume),a

	ld b,0

	ld a,h
	cp MAX_FREQUENCY/&100+1
	jr nc,psg_send_data_reg0_overflow_correction
	cp MAX_FREQUENCY/&100
	jr z,psg_send_data_reg0_overflow
	or a
	jr nz,psg_send_data_reg0_not_zero
	ld a,l

psg_send_data_reg0_fd
	cp &fd
	jr nc,psg_send_data_reg0_not_zero

	or a
	jr z,find_frequency_end
	jr find_frequency

psg_send_data_reg0_overflow
	ld a,l
	cp MAX_FREQUENCY
	jr c,psg_send_data_reg0_fd

psg_send_data_reg0_overflow_correction
	ld h,&07
	ld a,l
	and &77
	ld l,a

psg_send_data_reg0_not_zero
	srl h
	rr l
	inc b

	ld a,h
	or a
	jr nz,psg_send_data_reg0_not_zero
	ld a,l
	cp &fd
	jr nc,psg_send_data_reg0_not_zero

	or a
	jr z,find_frequency_end

find_frequency
	cp &0f
	jr c,find_frequency_end
	srl a
	inc b
	cp &0f
	jr c,find_frequency_end
	srl a
	inc b
	cp &0f
	jr c,find_frequency_end
	srl a
	inc b
	cp &0f
	jr c,find_frequency_end
	srl a
	inc b
	cp &0f
	jr c,find_frequency_end
frequency_zero
	xor a		; a = 0
find_frequency_end
	ld hl,frequence_table_analyzer_value
	ld c,a
	ld a,b		; a = octave
	ld b,&00
	ld d,b
	add hl,bc

	ld e,(hl)	; frequency value

	ld c,a		; c = octave
	sla c

draw_spectrum_analyzer_address equ $+1
	ld hl,draw_spectrum_analyzer_first_address_table
	add hl,bc
	ld a,(hl)
	inc hl
	ld h,(hl)
	ld l,a		; hl = screen_address

	add hl,de	; add pos_x

psg_channel_volume equ $ +1
	ld a,0		; <> 0

	ld c,&0f

	cp c
;	jr c,draw_freq_column_carry_10
	jr z,draw_freq_column_carry_10
	dec a
draw_freq_column_carry_10
	ld d,a

draw_freq_pixel equ $+1
	ld b,&aa

draw_freq_column
	ld a,(hl)
	or b
	ld (hl),a

	ld a,h
	sub &08
	ld h,a
	and &38
	cp &38
	jr nz,draw_freq_column_continue
	ld a,h
	add &40
	ld h,a
	ld a,l
	sub screen_width
	ld l,a
	jr nc,draw_freq_column_continue
	ld a,h
	dec h
	and &07
	jr nz,draw_freq_column_continue
	ld a,h
	add &08
	ld h,a

draw_freq_column_continue
	dec c
	dec d
	jp p,draw_freq_column

erase_freq_column
	ld a,(hl)
	and b
	ret z
	xor b
	ld (hl),a

erase_freq_column_no
	ld a,h
	sub &08
	ld h,a
	and &38
	cp &38
	jr nz,erase_freq_column_continue
	ld a,h
	add &40
	ld h,a
	ld a,l
	sub screen_width
	ld l,a
	jr nc,erase_freq_column_continue
	ld a,h
	dec h
	and &07
	jr nz,erase_freq_column_continue
	ld a,h
	add &08
	ld h,a

erase_freq_column_continue
	dec c
	jp p,erase_freq_column

	ret
; ---------------------------
draw_freq_spectrum_analyzer_first_address
; ---------------------------
	ld hl,draw_spectrum_analyzer_first_address-&0800
	ld de,draw_spectrum_analyzer_first_address
	ld bc,96-1
	ldir
	ld a,(hl)
	ld (de),a
	ld d,h
	ld e,l
	ld h,draw_spectrum_analyzer_first_address-&1000+95/&100
	ld bc,96-1
	lddr
	ld a,(hl)
	ld (de),a
	ld d,h
	ld e,l
	ld h,draw_spectrum_analyzer_first_address-&1800/&100
	ld bc,96-1
	ldir
	ld a,(hl)
	ld (de),a
	ld d,h
	ld e,l
	ld h,draw_spectrum_analyzer_first_address-&2000+95/&100
	ld bc,96-1
	lddr
	ld a,(hl)
	ld (de),a
	ld d,h
	ld e,l
	ld h,draw_spectrum_analyzer_first_address-&2800/&100
	ld bc,96-1
	ldir
	ld a,(hl)
	ld (de),a
	ld d,h
	ld e,l
	ld h,draw_spectrum_analyzer_first_address-&3000+95/&100
	ld bc,96-1
	lddr
	ld a,(hl)
	ld (de),a
	ld d,h
	ld e,l
	ld h,draw_spectrum_analyzer_first_address-&3800/&100
	ld bc,96-1
	ldir
	ld a,(hl)
	ld (de),a
	ld d,h
	ld e,l
	ld hl,draw_spectrum_analyzer_first_address-screen_width+95
	ld bc,96-1
	lddr
	ld a,(hl)
	ld (de),a
	ld h,draw_spectrum_analyzer_first_address-screen_width-&0800/&100
	ld de,draw_spectrum_analyzer_first_address-screen_width
	ld bc,96-1
	ldir
	ld a,(hl)
	ld (de),a
	ld d,h
	ld e,l
	ld h,draw_spectrum_analyzer_first_address-screen_width-&1000+95/&100
	ld bc,96-1
	lddr
	ld a,(hl)
	ld (de),a
	ld d,h
	ld e,l
	ld h,draw_spectrum_analyzer_first_address-screen_width-&1800/&100
	ld bc,96-1
	ldir
	ld a,(hl)
	ld (de),a
	ld d,h
	ld e,l
	ld h,draw_spectrum_analyzer_first_address-screen_width-&2000+95/&100
	ld bc,96-1
	lddr
	ld a,(hl)
	ld (de),a
	ld d,h
	ld e,l
	ld h,draw_spectrum_analyzer_first_address-screen_width-&2800/&100
	ld bc,96-1
	ldir
	ld a,(hl)
	ld (de),a
	ld d,h
	ld e,l
	ld h,draw_spectrum_analyzer_first_address-screen_width-&3000+95/&100
	ld bc,96-1
	lddr
	ld a,(hl)
	ld (de),a
	ld d,h
	ld e,l
	ld h,draw_spectrum_analyzer_first_address-screen_width-&3800/&100
	ld bc,96-1
	ldir
	ld a,(hl)
	ld (de),a
	ld d,h
	ld e,l
	ld hl,draw_spectrum_analyzer_first_address-screen_width-screen_width+95
	ld bc,96-1
	lddr
	ld a,(hl)
	ld (de),a
	ret
; ---------------------------
draw_freq_spectrum_analyzer_second_address
; ---------------------------
	ld hl,draw_spectrum_analyzer_second_address-&0800
	ld de,draw_spectrum_analyzer_second_address
	ld bc,96-1
	ldir
	ld a,(hl)
	ld (de),a
	ld d,h
	ld e,l
	ld h,draw_spectrum_analyzer_second_address-&1000+95/&100
	ld bc,96-1
	lddr
	ld a,(hl)
	ld (de),a
	ld d,h
	ld e,l
	ld h,draw_spectrum_analyzer_second_address-&1800/&100
	ld bc,96-1
	ldir
	ld a,(hl)
	ld (de),a
	ld d,h
	ld e,l
	ld h,draw_spectrum_analyzer_second_address-&2000+95/&100
	ld bc,96-1
	lddr
	ld a,(hl)
	ld (de),a
	ld d,h
	ld e,l
	ld h,draw_spectrum_analyzer_second_address-&2800/&100
	ld bc,96-1
	ldir
	ld a,(hl)
	ld (de),a
	ld d,h
	ld e,l
	ld h,draw_spectrum_analyzer_second_address-&3000+95/&100
	ld bc,96-1
	lddr
	ld a,(hl)
	ld (de),a
	ld d,h
	ld e,l
	ld h,draw_spectrum_analyzer_second_address-&3800/&100
	ld bc,96-1
	ldir
	ld a,(hl)
	ld (de),a
	ld d,h
	ld e,l
	ld hl,draw_spectrum_analyzer_second_address-screen_width+95
	ld bc,96-1
	lddr
	ld a,(hl)
	ld (de),a
	ld h,draw_spectrum_analyzer_second_address-screen_width-&0800/&100
	ld de,draw_spectrum_analyzer_second_address-screen_width
	ld bc,96-1
	ldir
	ld a,(hl)
	ld (de),a
	ld d,h
	ld e,l
	ld h,draw_spectrum_analyzer_second_address-screen_width-&1000+95/&100
	ld bc,96-1
	lddr
	ld a,(hl)
	ld (de),a
	ld d,h
	ld e,l
	ld h,draw_spectrum_analyzer_second_address-screen_width-&1800/&100
	ld bc,96-1
	ldir
	ld a,(hl)
	ld (de),a
	ld d,h
	ld e,l
	ld h,draw_spectrum_analyzer_second_address-screen_width-&2000+95/&100
	ld bc,96-1
	lddr
	ld a,(hl)
	ld (de),a
	ld d,h
	ld e,l
	ld h,draw_spectrum_analyzer_second_address-screen_width-&2800/&100
	ld bc,96-1
	ldir
	ld a,(hl)
	ld (de),a
	ld d,h
	ld e,l
	ld h,draw_spectrum_analyzer_second_address-screen_width-&3000+95/&100
	ld bc,96-1
	lddr
	ld a,(hl)
	ld (de),a
	ld d,h
	ld e,l
	ld h,draw_spectrum_analyzer_second_address-screen_width-&3800/&100
	ld bc,96-1
	ldir
	ld a,(hl)
	ld (de),a
	ld d,h
	ld e,l
	ld hl,draw_spectrum_analyzer_second_address-screen_width-screen_width+95
	ld bc,96-1
	lddr
	ld a,(hl)
	ld (de),a
	ret
; ---------------------------
draw_freq_spectrum_analyzer_third_address
; ---------------------------
	ld hl,draw_spectrum_analyzer_third_address-&0800
	ld de,draw_spectrum_analyzer_third_address
	ld bc,96-1
	ldir
	ld a,(hl)
	ld (de),a
	ld d,h
	ld e,l
	ld h,draw_spectrum_analyzer_third_address-&1000+95/&100
	ld bc,96-1
	lddr
	ld a,(hl)
	ld (de),a
	ld d,h
	ld e,l
	ld h,draw_spectrum_analyzer_third_address-&1800/&100
	ld bc,96-1
	ldir
	ld a,(hl)
	ld (de),a
	ld d,h
	ld e,l
	ld h,draw_spectrum_analyzer_third_address-&2000+95/&100
	ld bc,96-1
	lddr
	ld a,(hl)
	ld (de),a
	ld d,h
	ld e,l
	ld h,draw_spectrum_analyzer_third_address-&2800/&100
	ld bc,96-1
	ldir
	ld a,(hl)
	ld (de),a
	ld d,h
	ld e,l
	ld h,draw_spectrum_analyzer_third_address-&3000+95/&100
	ld bc,96-1
	lddr
	ld a,(hl)
	ld (de),a
	ld d,h
	ld e,l
	ld h,draw_spectrum_analyzer_third_address-&3800/&100
	ld bc,96-1
	ldir
	ld a,(hl)
	ld (de),a
	ld d,h
	ld e,l
	ld hl,draw_spectrum_analyzer_third_address-screen_width+95
	ld bc,96-1
	lddr
	ld a,(hl)
	ld (de),a
	ld h,draw_spectrum_analyzer_third_address-screen_width-&0800/&100
	ld de,draw_spectrum_analyzer_third_address-screen_width
	ld bc,96-1
	ldir
	ld a,(hl)
	ld (de),a
	ld d,h
	ld e,l
	ld h,draw_spectrum_analyzer_third_address-screen_width-&1000+95/&100
	ld bc,96-1
	lddr
	ld a,(hl)
	ld (de),a
	ld d,h
	ld e,l
	ld h,draw_spectrum_analyzer_third_address-screen_width-&1800/&100
	ld bc,96-1
	ldir
	ld a,(hl)
	ld (de),a
	ld d,h
	ld e,l
	ld h,draw_spectrum_analyzer_third_address-screen_width-&2000+95/&100
	ld bc,96-1
	lddr
	ld a,(hl)
	ld (de),a
	ld d,h
	ld e,l
	ld h,draw_spectrum_analyzer_third_address-screen_width-&2800/&100
	ld bc,96-1
	ldir
	ld a,(hl)
	ld (de),a
	ld d,h
	ld e,l
	ld h,draw_spectrum_analyzer_third_address-screen_width-&3000+95/&100
	ld bc,96-1
	lddr
	ld a,(hl)
	ld (de),a
	ld d,h
	ld e,l
	ld h,draw_spectrum_analyzer_third_address-screen_width-&3800/&100
	ld bc,96-1
	ldir
	ld a,(hl)
	ld (de),a
	ld d,h
	ld e,l
	ld hl,draw_spectrum_analyzer_third_address-screen_width-screen_width+95
	ld bc,96-1
	lddr
	ld a,(hl)
	ld (de),a
	ret
;-------------------------------------------------------------------------------
.interrupt
;-------------------------------------------------------------------------------
	di
	push af
	push bc
	push de
	push hl
	push ix
	push iy

	ld hl,interrupt_next_counter+1

	ld b,&f5
	in a,(c)
	rra
	jr nc,interrupt_next
	ld (hl),&0
	jr interrupt_next_counter
interrupt_next
	defs 2,0
	inc (hl)
interrupt_next_counter
	ld a,&ff
	cp 6
	jr c,interrupt_next_bad_crtc
	xor a
interrupt_next_bad_crtc
	ld (hl),a

	ld hl,interrupt_list
	ld b,&00
	add a,a
	ld c,a
	add hl,bc
	ld e,(hl)
	inc hl
	ld d,(hl)
	ex de,hl
	jp (hl)

interrupt_list
	dw interrupt_Sense_Interrupt
	dw interrupt_Sense_Interrupt
	dw interrupt_Sense_Interrupt
	dw interrupt_Sense_Interrupt
	dw interrupt_Sense_Interrupt
	dw interrupt_Sense_Interrupt

.interrupt_Sense_Interrupt
	call Sense_Interrupt

interrupt_back
	pop iy
	pop ix
	pop hl
	pop de
	pop bc
	pop af
	ei
	reti
;-------------------------------------------------------------------------------
.PRINT_TEXT_CRx2
;-------------------------------------------------------------------------------
	call PRINT_TEXT_CR
;-------------------------------------------------------------------------------
.PRINT_TEXT_CR
;-------------------------------------------------------------------------------
;	push hl
	ex de,hl
	ld hl,text_cr
	call PRINT_TEXT
	ex de,hl
;	pop hl
	ret
;-------------------------------------------------------------------------------
.print_intro_text
;-------------------------------------------------------------------------------
	ld a,(set_music_loop_test)
	or a
	jr z,text_music_repeat_notactive
	ld hl,text_music_repeat
	call PRINT_TEXT
text_music_repeat_notactive

	ld a,(force_currentmusic_duration_active)
	or a
	jr z,text_music_force_end_notactive
	ld hl,text_music_force_end
	call PRINT_TEXT
text_music_force_end_notactive

	ld hl,text_intro
	jp PRINT_TEXT
;-------------------------------------------------------------------------------
.l0000
call_play_music equ &0000
;-------------------------------------------------------------------------------
	call bank_0

call_play_music_adr equ &0004	; $ +1
	call 0		; call play_music_adr

music_end_address equ &0007 ; $+1
	ld hl,0
	ld a,h
	or l
	jr z,not_exist_music_end
	ld a,(hl)
	db #18,#09	; jr bank_5
not_exist_music_end
	dec a
	db #18,#06	; jr bank_5
; ---------------------------
call_init_music equ &0013
; ---------------------------
	call bank_0

call_init_music_adr equ &0017	; $ +1
	call 0		; call init_music_adr

;	jp bank_5
; ---------------------------
bank_5 equ &0019
; ---------------------------
	ld bc,&7fc5
	out (c),c
	ret
; ---------------------------
bank_0 equ &001f
; ---------------------------
	push bc
	ld bc,&7fc0
	out (c),c
	pop bc
	ret
; ---------------------------
ldir_music equ &0027
; ---------------------------
	call bank_0
	ldir
	db #18,#eb	; jr bank_5
; ---------------------------
lddr_music equ &002e
; ---------------------------
	call bank_0
	lddr
	db #18,#e4	; jr bank_5
;-------------------------------------------------------------------------------
; Print Manager
;-------------------------------------------------------------------------------
	read	"..\Common_Sources\Print_Manager_v1_1.asm"
;-------------------------------------------------------------------------------
; CRTC Manager
;-------------------------------------------------------------------------------
	read	"..\Common_Sources\CRTC_Manager_v1_0.asm"
	read	"CPCMix_Menu_CRTC_data_v1_0.asm"
;-------------------------------------------------------------------------------
; Ay Sound Manager
;-------------------------------------------------------------------------------
	read	"..\Common_Sources\Ay_Sound_Manager_v1_0.asm"
;-------------------------------------------------------------------------------
; Keyboard Manager
;-------------------------------------------------------------------------------
	read	"..\Common_Sources\Keyboard_Manager_v1_0.asm"
;-------------------------------------------------------------------------------
; Time Counter
;-------------------------------------------------------------------------------
	read	"..\Common_Sources\Time_Counter_v1_0.asm"
;-------------------------------------------------------------------------------
	IF USE_COMPACTER

	read	"..\Common_Sources\Bitbuste_1_2_DeComp_v1_0.asm"

	ENDIF
;-------------------------------------------------------------------------------
; Menu data
;-------------------------------------------------------------------------------
	read	"CPCMix_Menu_data_v1_1.asm"
;-------------------------------------------------------------------------------
; FDC Read
;-------------------------------------------------------------------------------
	read	"..\Common_Sources\FDC_v2_e.asm"
;-------------------------------------------------------------------------------
; End Of File
;-------------------------------------------------------------------------------
EOF_ADDRESS
LIST
EOF_LENGTH equ EOF_ADDRESS-&4000
NOLIST
CLOSE
; ---------------------------
.end_player equ $
; ---------------------------
